﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

class AutoGunnery : Weapon
{
    float m_ContinueFireTime = 0.1f;
    float m_ContinueFireTimeCount = 0;
    int m_MultiFire = 0;
    GameObject m_MultiFireShip;
    Vector3 m_MultiFirePosition;
    Vector3 m_MultiFireDirection;
    public AutoGunnery()
    {
        m_Price = 600;
        m_Type = ItemsType.WEAPON_MIDDLE_KINETICS_AUTO_GUNNERY;
        m_Speciality = Speciality.MAIN;
        ResearchInfluence = InfluenceMG.INFLUENCE_EARTH;
        ShutRange = 15;
        ShutVelocity = 5.7f;
        BulletFlyVelocity = 15;
        Damage = 140;
        TurnSpeed = 80f;
        m_DropRate = 0.08f;
        m_ExplotionScale = 0.4f;
        Name = "双联装58mm自动炮";
        Perview = "Textures/Weapon/WeaponSmall/AutoGunnery";
        WeaponResource = "Perfabs/Weapon/Small/AutoGunnery";
        BulletResource = "Perfabs/Bullet/FireGunneryBullet";
        m_FireAudioResouce = "Perfabs/Audio/Pang";
        m_Level = Level.LEVEL_MIDDLE;
        Description = "出自于地球联合数字重工，为第一次太阳系战争中远距离快速打击小型单位而研制，双联装制就是为了一次瞄准就可以击破小型护卫舰的偏射护盾，常常用在驱逐舰远距离支援使用。";

    }
    public override FlyItem Fire(GameObject ship, WeaponSlot slot, Vector3 position, Vector3 direction)
    {
        if (ship)
            m_Ship = ship.GetComponent<Ship>();
        Slot = slot;
        var horizontalDir = Vector3.Cross(m_MultiFireDirection, new Vector3(0, 0, 1));
        var offset = horizontalDir * 0.1f;
        m_MultiFireShip = ship;
        m_MultiFirePosition = position;
        m_MultiFireDirection = direction;
        m_MultiFire = 1;
        m_ContinueFireTime = UnityEngine.Random.Range(0.0001f, 0.08f);
        return base.Fire(ship, slot, position - offset, direction);
    }


    public override int update()
    {
        base.update();

        if (m_MultiFire > 0)
        {
            m_ContinueFireTimeCount += Time.deltaTime;
            if (m_ContinueFireTimeCount >= m_ContinueFireTime)
            {
                var horizontalDir = Vector3.Cross(m_MultiFireDirection, new Vector3(0, 0, 1));
                var offset = horizontalDir * 0.1f;
                m_MultiFire--;
                m_ContinueFireTimeCount = 0;
                base.Fire(m_MultiFireShip, Slot, m_MultiFirePosition + offset, m_MultiFireDirection);
            }
        }
        return 1;
    }

}